package com.amtf.datafile2.core.txt;

import java.text.DateFormatSymbols;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Locale;

import com.amtf.datafile2.DataField;
import com.amtf.datafile2.DataFieldFormat;

public class TxtDataFieldFormatDatetime implements DataFieldFormat {
	private SimpleDateFormat format = null;

	public TxtDataFieldFormatDatetime(String pattern) {
		format = new SimpleDateFormat(pattern);
	}

	public TxtDataFieldFormatDatetime(String pattern, DateFormatSymbols formatSymbols) {
		format = new SimpleDateFormat(pattern, formatSymbols);
	}

	public TxtDataFieldFormatDatetime(String pattern, Locale locale) {
		format = new SimpleDateFormat(pattern, locale);
	}

	public TxtDataFieldFormatDatetime(SimpleDateFormat dateFormat) {
		this.format = dateFormat;
	}

	public String format(DataField value) {
		return format(value.getValue());
	}

	public String format(Object value) {
		return format.format(value);
	}

	public String format(String value) {
		try {
			return format.format(format.parse(value));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return value;
	}
}
